import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: { path: 'el' }
  },
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/user',
    component: () => import('../layout/MainLayout.vue'),
    meta: { name: '基础信息' },
    children: [
      {
        path: '',
        redirect: { path: 'userList' }
      },
      {
        path: 'userList',
        component: () => import('../views/user/UserList.vue'),
        meta: { name: '用户管理' }
      },
      {
        path: 'addUser',
        component: () => import('../views/user/UserForm.vue'),
        meta: { name: '新增用户' }
      },
      {
        path: 'editUser',
        component: () => import('../views/user/UserForm.vue'),
        meta: { name: '修改用户' }
      }
    ]
  },
  {
    path: '/el',
    component: () => import('../layout/MainLayout.vue'),
    meta: { name: 'Element' },
    children: [
      {
        path: '',
        redirect: { path: 'el1' }
      },
      {
        path: 'el1',
        component: () => import('../views/elements/el1.vue'),
        meta: { name: 'Element-UI表格' }
      },
      {
        path: 'el2',
        component: () => import('../views/elements/el2.vue'),
        meta: { name: 'Element-UI表单1' }
      },
      {
        path: 'el3',
        component: () => import('../views/elements/el3.vue'),
        meta: { name: 'Element-UI表单2' }
      },
      {
        path: 'uploadFile',
        component: () => import('../views/elements/UploadFile.vue'),
        meta: { name: '上传图片' }
      }
    ]
  },
  {
    path: '/echarts',
    component: () => import('../layout/MainLayout.vue'),
    meta: { name: 'ECharts' },
    children: [
      {
        path: '',
        redirect: { path: 'ePage1' }
      },
      {
        path: 'ePage1',
        component: () => import('../views/echarts/ePage1.vue'),
        meta: { name: 'echarts1' }
      },
      {
        path: 'ePage2',
        component: () => import('../views/echarts/ePage2.vue'),
        meta: { name: 'echarts2' }
      }
    ]
  },
  {
    path: '/test',
    component: () => import('../layout/MainLayout.vue'),
    meta: { name: '测试' },
    children: [
      {
        path: '',
        redirect: { path: 'testVuex' }
      },
      {
        path: 'testVuex',
        component: () => import('../views/test/TestVuex.vue'),
        meta: { name: '测试VUEX' }
      },
      {
        path: 'cP',
        component: () => import('../views/test/CP.vue'),
        meta: { name: '测试父子组件通信' }
      }
    ]
  }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return (originalPush.call(this, location)).catch((err) => {
    console.log(err)
    // window.location.reload()
  })
}

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

export default router
